package org.djw.org.service.impl;

import org.djw.basic.exception.BusinessException;
import org.djw.basic.service.impl.BaseServiceImpl;
import org.djw.org.domain.Employee;
import org.djw.org.domain.Shop;
import org.djw.org.mapper.EmployeeMapper;
import org.djw.org.mapper.ShopMapper;
import org.djw.org.service.IEmployeeService;
import org.djw.org.service.IShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;


/**
 * 员工模块业务层
 */
@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {
    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private IEmployeeService employeeService;

    @Override
    public void add(Shop shop) {
        //先判断两次的密码是否一致
        //获取admin对象
        Employee admin = shop.getAdmin();
        if (!admin.getPassword().equals(admin.getComfirmPassword())){
            throw new BusinessException("两次密码不一致,请重新输入!!");
        }
        //判断用户名是否存在
        Employee employee = employeeMapper.loadByUsername(admin.getUsername());
        if (employee != null){
            throw new BusinessException("用户名已存在,请重新输入");
        }
        //先保存admin对象并且返回自增id  employee_id
        employeeService.add(admin);
        //设置给admin并保存shop  返回自增id shop_id
        shop.setAdmin(admin);
        shopMapper.save(shop);
        //修改employee
        admin.setShop_id(shop.getId());
        employeeMapper.update(admin);
    }
}
